/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "ODE几何.h"


#include <ppl.h>
#include <矩阵.h>
#include <三角形.h>




S_ODE平面几何体::S_ODE平面几何体(S_ODE核心* core, const vec4& ckg, float32 密度) {
	//m_物理引擎枚举 = E_物理引擎::E_物理引擎_ODE;
	m_Type = E_物理碰撞几何类型::E_碰撞几何_平面;

	m_Shape = dCreatePlane(core->m_物理空间, ckg.x, ckg.y, ckg.z, 0.0);

	//dGeomPlaneSetParams(m_Shape, n[0], n[1], n[2], d);
}

S_ODE平面几何体::~S_ODE平面几何体()
{
}












S_ODE长方体几何体::S_ODE长方体几何体(S_ODE核心* core, const vec3& ckg, float32 密度) {
	//m_物理引擎枚举 = E_物理引擎::E_物理引擎_ODE;
	m_Type = E_物理碰撞几何类型::E_碰撞几何_方体;
	
	m_Shape = dCreateBox(core->m_物理空间, fabs(ckg.x), fabs(ckg.y), fabs(ckg.z));
}

S_ODE长方体几何体::~S_ODE长方体几何体() {
	dGeomDestroy(m_Shape);
}



 



S_ODE凸壳几何体::S_ODE凸壳几何体(S_ODE核心* core, const S_三角Mesh& v, float32 密度) {
	//m_物理引擎枚举 = E_物理引擎::E_物理引擎_ODE;
	m_Type = E_物理碰撞几何类型::E_碰撞几何_凸壳;
	
	uint32 num = v.m_vNum;

	std::vector<float64> points(v.m_iNum * 3);
	auto* r = points.data();
	auto* p = v.m_vert;
	
	Concurrency::parallel_for<uint32>(0, num, [&](uint32 i) {
		r[i*3]		= p[i].x;
		r[i*3+1]	= p[i].y;
		r[i*3+2]	= p[i].z;
	});
	
	std::vector<vec4_64> planes(v.m_iNum);
	auto* p_planes = planes.data();
	Concurrency::parallel_for<uint32>(0, v.m_iNum, [&](uint32 i) {
		r[i * 3] = p[i].x;
		r[i * 3 + 1] = p[i].y;
		r[i * 3 + 2] = p[i].z;

		S_三角形_ triangle;
		vec3 normal;

		triangle.pointA = v.m_vert[v.m_index[i].x];
		triangle.pointB = v.m_vert[v.m_index[i].y];
		triangle.pointC = v.m_vert[v.m_index[i].z];
		geometry_TriangleNormal(triangle, (float32*)&normal);

		float32 o[3] = { 0.0f,0.0f,0.0f };
		p_planes[i].x = normal.x;
		p_planes[i].y = normal.y;
		p_planes[i].z = normal.z;
		p_planes[i].w = f_点到平面距离(triangle, o, (float32*)&normal);
	});


	m_Shape = dCreateConvex(core->m_物理空间, (float64*)p_planes, v.m_iNum, r, v.m_iNum, (uint32*)v.m_index);

}

S_ODE凸壳几何体::~S_ODE凸壳几何体() {
	dGeomDestroy(m_Shape);
}

